﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Zyg.SSO.Core.Business
{
    public class RolePermission : BusinessBase<RolePermission, int>
    {
        #region Fields and Properties

        private Guid roleid;
        private Guid permissionid;

        public Guid RoleID
        {
            get { return roleid; }
            set { roleid = value; }
        }
        public Guid PermissionID
        {
            get { return permissionid; }
            set { permissionid = value; }
        }

        #endregion

        protected override void ValidationRules()
        {
        }

        protected override RolePermission DataSelect(int id)
        {
            return null;
        }

        protected override void DataUpdate()
        {
        }

        protected override void DataInsert()
        {
            Zyg.SSO.Core.Services.DALService.InsertRolePermission(this);
        }

        protected override void DataDelete()
        {
        }

        public static void UpdateRolePermission(Business.Role role, List<RolePermission> rolepermissions)
        {
            if (role.Id == Role.SuperAdmin.Id || role.Id == Role.User.Id)
            {
                return;
            }

            Zyg.SSO.Core.Services.DALService.UpdateRolePermission(role, rolepermissions);
        }

        public static List<Permission> SelectPermissionByRoleID(Guid rolrid)
        {
            return Zyg.SSO.Core.Services.DALService.SelectPermissionByRoleID(rolrid);
        }
    }

    public class Permission
    {
        #region Constructors

        static Permission()
        {
            RoleManage = new Permission(new Guid("275eb1c5-0be9-4554-aa3a-8f702bc7936a"), "角色管理");
            AppManage = new Permission(new Guid("8c5c9464-cd6c-4175-84ae-d9e0dfb03dd8"), "应用系统管理");
            AdminManage = new Permission(new Guid("8c7205b7-0117-4300-b4f2-f96eeb4fab33"), "管理员管理");
            AccountManage = new Permission(new Guid("79069953-652a-42c5-aab1-f0ef3217b6fa"), "用户管理");
            SettingManage = new Permission(new Guid("08607b2d-38f0-479a-bbcb-c59e2bf2efb9"), "系统设置");
            LogManage = new Permission(new Guid("d41fac64-e348-420e-9523-9dc458c9dfd6"), "日志管理");
        }

        protected Permission() { }

        private Permission(Guid id, String name)
        {
            this.id = id;
            this.name = name;
        }

        #endregion

        #region Fields and Properties

        private Guid id;
        private string name;
        private DateTime datecreated;
        private int type;
        private string remark;
        private Guid moduleid;

        /// <summary>
        /// 标识符
        /// </summary>
        public Guid ID
        {
            get { return id; }
            set { id = value; }
        }
        /// <summary>
        /// 权限名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// 添加日期
        /// </summary>
        public DateTime DateCreated
        {
            get { return datecreated; }
            set { datecreated = value; }
        }
        /// <summary>
        /// 权限类型
        /// </summary>
        public int Type
        {
            get { return type; }
            set { type = value; }
        }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark
        {
            get { return remark; }
            set { remark = value; }

        }
        /// <summary>
        /// 模块ID
        /// </summary>
        public Guid MoudleID
        {
            get { return moduleid; }
            set { moduleid = value; }
        }

        #endregion

        #region Static Instances

        public static readonly Permission RoleManage = null;

        public static readonly Permission AppManage = null;

        public static readonly Permission AdminManage = null;

        public static readonly Permission SettingManage = null;

        public static readonly Permission LogManage = null;

        /// <summary>
        /// 用户管理
        /// </summary>
        public static readonly Permission AccountManage = null;

        #endregion

        #region Operators

        public static implicit operator Permission(string id)
        {
            switch (id.ToLower())
            {
                case "79069953-652a-42c5-aab1-f0ef3217b6fa":
                    return Permission.AccountManage;
                case "275eb1c5-0be9-4554-aa3a-8f702bc7936a":
                    return Permission.RoleManage;
                case "8c7205b7-0117-4300-b4f2-f96eeb4fab33":
                    return Permission.AdminManage;
                case "8c5c9464-cd6c-4175-84ae-d9e0dfb03dd8":
                    return Permission.AppManage;
                case "08607b2d-38f0-479a-bbcb-c59e2bf2efb9":
                    return Permission.SettingManage;
                case "d41fac64-e348-420e-9523-9dc458c9dfd6":
                    return Permission.LogManage;
                default:
                    throw new Exception("权限转换错误");
            }
        }

        public static implicit operator Permission(Guid id)
        {
            switch (id.ToString().ToLower())
            {
                case "79069953-652a-42c5-aab1-f0ef3217b6fa":
                    return Permission.AccountManage;
                case "275eb1c5-0be9-4554-aa3a-8f702bc7936a":
                    return Permission.RoleManage;
                case "8c7205b7-0117-4300-b4f2-f96eeb4fab33":
                    return Permission.AdminManage;
                case "8c5c9464-cd6c-4175-84ae-d9e0dfb03dd8":
                    return Permission.AppManage;
                case "08607b2d-38f0-479a-bbcb-c59e2bf2efb9":
                    return Permission.SettingManage;
                case "d41fac64-e348-420e-9523-9dc458c9dfd6":
                    return Permission.LogManage;
                default:
                    throw new Exception("权限转换错误");
            }
        }

        public static explicit operator string(Permission permission)
        {
            if (permission != null)
            {
                return permission.ID.ToString();
            }
            else
                return string.Empty;
        }

        public static explicit operator Guid(Permission permission)
        {
            if (permission != null)
            {
                return permission.ID;
            }
            else
                return new Guid();
        }

        #endregion
    }
}
